import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:h_flutter_119/iconfont.dart';
import 'package:h_flutter_119/page/getx/test/extention/StringExt.dart';
import 'package:h_flutter_119/page/getx/test/extention/commonExt.dart';

///  防抖测试
///  ever 适合做监听、日志收集
///  debounce 适合做搜索输入框
class StateWorkersView extends StatelessWidget{
  StateWorkersView({super.key});

  final controller = CountController();

  @override
  Widget build(BuildContext context) {

    return Column(
      children: [
        GetX<CountController>(init: controller,builder: (controller) => Text("value => ${controller._count}"),initState: (state) {

        },),
        ElevatedButton(onPressed: () {
          controller.add();
        }, child: Text('add')),
        const Icon(Iconfont.iconarrowdown,color: Colors.blueAccent),

      ],
    );
  }
}

class CountController extends GetxController{

  final _count = 0.obs;

  get count => _count.value;
  set count(value) {
    _count.value = value;
  }

  add() => _count.value++;

  @override
  void onInit() {
    super.onInit();

    //每次
    ever(_count,(callback) {
      if (kDebugMode) {
        print("ever -> $callback");
      }
    });

    //第一次
    once(_count, (callback) => print("once -> " + callback.toString()));

    //防抖2秒内
    debounce(_count, (callback) => print("debounce -> " + callback.toString()),time: const Duration(seconds: 2));

    //定时器 1秒
    interval(_count, (callback) => print("interval -> ${callback.toString()}"),time: const Duration(seconds: 1));
  }

}
